/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/universe_ships.h"
#include "PlanetWars2dRT-core/specific/model/universe_observer.h"
#include "PlanetWars2dRT-core/specific/model/universe_observers.h"
#include "PlanetWars2dRT-core/specific/model/ship_position.h"
#include "PlanetWars2dRT-core/specific/model/ship_life.h"
#include "PlanetWars2dRT-core/utils/collision_2d_uint.h"


EXTERN_C_BEGIN


void
universe_notify_nb_ships_changed(universe_t* a_universe)
{
  struct observer_data_with_code data;
  data.code = UNIVERSE_OBSERVER_CODE_NB_SHIPS_CHANGED;
  data.data = NULL;
  universe_notify_observers(a_universe, &data);
}


void
universe_add_ship_no_check(universe_t*   a_universe,
			   const ship_t* ship_to_add)
{
  universe_add_ship_no_check_no_notify(a_universe, ship_to_add);
  universe_notify_nb_ships_changed(a_universe);
}

bool
universe_add_ship_unsafe_no_notify(universe_t*   a_universe,
				   const ship_t* ship_to_add)
{
  const position_2d_uint ship_pos = ship_get_top_left_position_current(ship_to_add);
  if(!(is_rectangle_in_size_2d_uint(&a_universe->size,
				    &ship_pos,
				    &ship_to_add->size)
       &&
       is_rectangle_in_size_2d_uint(&a_universe->size,
				    &ship_to_add->position_to_go,
				    &ship_to_add->size)))
    {
      return false;
    }
  universe_add_ship_no_check_no_notify(a_universe, ship_to_add);
  return true;
}

bool
universe_add_ship_unsafe(universe_t*   a_universe,
			 const ship_t* ship_to_add)
{
  if(universe_add_ship_unsafe_no_notify(a_universe, ship_to_add))
    {
      universe_notify_nb_ships_changed(a_universe);
      return true;
    }
  return false;
}

bool
universe_add_ship_no_notify(universe_t*   a_universe,
			    const ship_t* ship_to_add)
{
  return
    a_universe == NULL || ship_to_add == NULL
    ? false
    : universe_add_ship_unsafe_no_notify(a_universe, ship_to_add);
}

bool
universe_add_ship(universe_t*   a_universe,
		  const ship_t* ship_to_add)
{
  if(universe_add_ship_no_notify(a_universe, ship_to_add))
    {
      universe_notify_nb_ships_changed(a_universe);
      return true;
    }
  return false;
}


void
universe_remove_ships_no_notify(universe_t* a_universe)
{
  if(a_universe != NULL)
    {
      const size_t nb = universe_get_nb_ships_unsafe(a_universe);
      for(size_t i=0; i < nb; ++i)
	{
	  ship_destructor(universe_get_ship_no_check(a_universe, i));
	}
      VECTOR_GENERIC_STATIC_CLEAR(&a_universe->ships);
    }
}

void
universe_remove_ships(universe_t* a_universe)
{
  if(universe_get_nb_ships(a_universe) > 0)
    {
      universe_remove_ships_no_notify (a_universe);
      universe_notify_nb_ships_changed(a_universe);
    }
}

size_t
universe_remove_destroyed_ships_no_notify(universe_t* a_universe)
{
  size_t nb_destroyed_ships = 0;
  size_t nb_ships = universe_get_nb_ships(a_universe);
  if(nb_ships == 0)
    {
      return 0;
    }
  
  for(size_t i=0; i < nb_ships;)
    {
      ship_t* ship_i = universe_get_ship_no_check(a_universe, i);
      if(ship_is_destroyed(ship_i))
	{
	  ++nb_destroyed_ships;
	  --nb_ships;
	  
	  ship_destructor(ship_i);
	  *ship_i = * universe_get_ship_no_check(a_universe,
						 nb_ships);
	}
      else
	{
	  ++i;
	}
    }
  
  VECTOR_GENERIC_STATIC_RESIZE(&a_universe->ships,
			       nb_ships,
			       ship_t);
  return nb_destroyed_ships;
}

size_t
universe_remove_destroyed_ships(universe_t* a_universe)
{
  const size_t nb_destroyed_ships =
    universe_remove_destroyed_ships_no_notify(a_universe);
  if(nb_destroyed_ships > 0)
    {
      universe_notify_nb_ships_changed(a_universe);
    }
  return nb_destroyed_ships;
}


EXTERN_C_END
